package com.xyh.flowershop.controller;

import com.xyh.flowershop.annotations.RefreshToken;
import com.xyh.flowershop.service.RecevingCodeService;
import com.xyh.flowershop.vo.req.code.CodePageReqVo;
import com.xyh.flowershop.vo.req.code.CodeReqVo;
import com.xyh.flowershop.vo.resp.common.ResultRespVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author 小约翰可汗
 * @className RecevingCodeController
 * @createTime 2024/5/311:07
 */
@Slf4j
@RefreshToken
@RestController
@Tag(name = "收货地址接口")
@RequestMapping("/code/")
@CrossOrigin(origins = "*", maxAge = 3600)
public class RecevingCodeController {

    private final RecevingCodeService recevingCodeService;

    public RecevingCodeController(RecevingCodeService recevingCodeService) {
        this.recevingCodeService = recevingCodeService;
    }

    @PostMapping("addOrEdit")
    public ResultRespVo addOrEditRecevingCode(@RequestBody CodeReqVo vo){
        Boolean flag ;
        if(Objects.isNull(vo.getId())){
            flag = recevingCodeService.addRecevingCode(vo);
        }else{
            flag  =recevingCodeService.editRecevingCode(vo);
        }
        return flag ? ResultRespVo.success() : ResultRespVo.error();
    }

    @PostMapping("find/page")
    public ResultRespVo findPage(@RequestBody CodePageReqVo vo){
        return ResultRespVo.success(recevingCodeService.findPage(vo));
    }

    @DeleteMapping("delete/{id}")
    public ResultRespVo deleteCode(@PathVariable String id){
        List<String> ids = new ArrayList<>();
        ids.add(id);
        return recevingCodeService.deleteCode(ids) ? ResultRespVo.success() : ResultRespVo.error();
    }

    @GetMapping("find/{id}")
    public ResultRespVo findCode(@PathVariable String id){
        return ResultRespVo.success(recevingCodeService.findCode(id));
    }
}
